package com.maimao.kill.controller;

import com.maimao.api.kill.ScheduleApi;
import com.maimao.core.model.resp.PageResult;
import com.maimao.core.model.resp.Result;
import com.maimao.kill.controller.base.BaseController;
import com.maimao.kill.service.ScheduleService;
import com.maimao.model.common.ScheduleStatusInput;
import com.maimao.model.kill.Schedule;
import com.maimao.model.kill.enums.ScheduleCreateType;
import com.maimao.model.kill.input.ScheduleInput;
import com.maimao.model.kill.input.ScheduleTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author MaoLin Wang
 * @date 2021/4/7 9:55 下午
 */
@RestController
@RequestMapping("/kill/schedule")
public class ScheduleController extends BaseController implements ScheduleApi {

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 后台查询
     * @param input
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/page-list")
    @Override
    public Result<?> sysPageList(@RequestBody ScheduleInput input,
                                              @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                              @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        input.setPage(page);
        input.setSize(size);
        PageResult<Schedule> pageResult = scheduleService.list(input);
        return Result.okData(pageResult);
    }

    /**
     * 商家报名查询可报名排期
     * 大于当日的、enabled=1的，如果过了15点则查询后天的
     * @param input
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/usable-list")
    public Result<?> usableList(@RequestBody @Validated ScheduleInput input,
                                              @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                              @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        input.setPage(page);
        input.setSize(size);
        PageResult<Schedule> pageResult = scheduleService.usableList(input);
        return Result.okData(pageResult);
    }

    /**
     * 根据模板创建排期
     */
    @Override
    @PostMapping("/addFromTem")
    public Result addByTemplate(@RequestBody @Validated ScheduleTemplate template) {
        Result result = checkTemplateAndGetSchedule(template);
        if (result != null) {
            return result;
        }
        boolean res = scheduleService.addByTemplate(template, ScheduleCreateType.MANUAL.getStatus());
        return res ? Result.ok("创建成功") : Result.notValid("活动不存在");
    }

    /**
     * 开启/停止排期
     * @param input
     * @return
     */
    @Override
    @PostMapping("/update-status")
    public Result updateStatus(@RequestBody @Validated ScheduleStatusInput input) {
        return scheduleService.updateStatus(input);
    }

    private Result checkTemplateAndGetSchedule(ScheduleTemplate template) {
        Integer cycle = template.getCycle();
        Integer dayStartHour = template.getDayStartHour();
        Integer dayEndHour = template.getDayEndHour();

        int totalHour = dayEndHour - dayStartHour;
        if (totalHour <= 0) {
            return Result.notValid("每日排期至少为1小时");
        }
        if (totalHour < cycle) {
            return Result.notValid("总时长应大于1个周期");
        }
        LocalDate now = LocalDate.now();
        if (now.compareTo(template.getStartTime()) >= 0) {
            return Result.notValid("只能排当日之后的日期");
        }
        return null;

    }


}
